# trace generated using paraview version 5.10.1
#import paraview
#paraview.compatibility.major = 5
#paraview.compatibility.minor = 10

#### import the simple module from the paraview
from paraview.simple import *
import concurrent.futures
import os
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

def convert_one_vts_to_stl(working_dir, id):
    # create a new 'XML Structured Grid Reader'
    vts = XMLStructuredGridReader(registrationName='vts', FileName=[working_dir+"/"+id+".vts"])

    # Properties modified
    vts.TimeArray = 'None'

    UpdatePipeline(time=0.0, proxy=vts)

    # create a new 'Iso Volume'
    isoVolume1 = IsoVolume(registrationName='IsoVolume1', Input=vts)

    # Properties modified on isoVolume1
    isoVolume1.ThresholdRange = [0.0, 50.0]

    UpdatePipeline(time=0.0, proxy=isoVolume1)

    # create a new 'Iso Volume'
    isoVolume2 = IsoVolume(registrationName='IsoVolume2', Input=isoVolume1)

    # Properties modified on isoVolume2
    isoVolume2.InputScalars = ['POINTS', 'define_marker_rho']
    isoVolume2.ThresholdRange = [0.001, 50.0]

    UpdatePipeline(time=0.0, proxy=isoVolume2)

    # create a new 'Iso Volume'
    isoVolume3 = IsoVolume(registrationName='IsoVolume3', Input=isoVolume2)

    # Properties modified on isoVolume3
    isoVolume3.InputScalars = ['POINTS', 'define_marker_theta']
    isoVolume3.ThresholdRange = [0.0, 50.0]

    UpdatePipeline(time=0.0, proxy=isoVolume3)

    # create a new 'Extract Surface'
    extractSurface1 = ExtractSurface(registrationName='ExtractSurface1', Input=isoVolume3)

    UpdatePipeline(time=0.0, proxy=extractSurface1)

    # save data
    SaveData(working_dir+"/"+id+".stl", proxy=extractSurface1, PointDataArrays=['u'])

    # destroy

    Delete(extractSurface1)
    del extractSurface1

    Delete(isoVolume3)
    del isoVolume3

    Delete(isoVolume2)
    del isoVolume2

    Delete(isoVolume1)
    del isoVolume1

    Delete(vts)
    del vts



def convert_all_vts_to_stl(working_dir):
    # Get all files in the directory
    files = os.listdir(working_dir)

    # Filter files with .vts extension and get only the file names
    vts_file_names = [os.path.splitext(file)[0]
                      for file in files if file.endswith('.vts')]

    # Create a ProcessPoolExecutor
    with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
        # Submit tasks to the executor
        futures = [executor.submit(convert_one_vts_to_stl, working_dir, id) for id in vts_file_names]

    # Wait for all tasks to complete
    concurrent.futures.wait(futures)

if __name__ == '__main__':
    result_save_path="Share/G47800/Model/DDPM/恒面-优化"
    convert_all_vts_to_stl(f"{result_save_path}")